package cn.johnyu.jacksonTest;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;


public class App 
{
    public static void main( String[] args ) throws Exception
    {
       
    	User u = new User(null,"john",100,345.678);
    	u.setBirth(new GregorianCalendar(1987, 9, 12).getTime());
    	Address a1=new Address("bj", u);
    	u.getAddrs().add(a1);
    	a1.setU(u);
    	
    	ObjectMapper mapper=new ObjectMapper();
        mapper.writeValue(System.out, u);
    }
}
//@JsonIgnoreProperties(value={"id","age"})忽略id,age
@JsonInclude(value=Include.NON_NULL)//ignore NULL
class User implements Serializable{
	@JsonIgnore//ignore id
	private Integer id;
	@JsonPropertyDescription(value="this is uname")
	private String name;
	private int age;
	@JsonSerialize()
	private double salary;
	
	
	@JsonFormat(pattern="yyyy-MM-dd")
	private Date birth;
	@JsonManagedReference
	private List<Address> addrs=new ArrayList<Address>();
	public User(Integer id, String name, int age,double salary) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.salary=salary;
	}
	
	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public List<Address> getAddrs() {
		return addrs;
	}

	public void setAddrs(List<Address> addrs) {
		this.addrs = addrs;
	}

	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	
	
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}

class Address{
	private String city;
	@JsonBackReference
	private User u;
	public Address(String city, User u) {
		super();
		this.city = city;
		this.u = u;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public User getU() {
		return u;
	}
	public void setU(User u) {
		this.u = u;
	}
	
}
